﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Gedea.WinForm.UserControls
{
    public partial class ClientControl : UserControl
    {
        public List<Control> myControls = new List<Control>();
        public ClientControl()
        {
            InitializeComponent();
        }

        private void ClientControl_Load(object sender, EventArgs e)
        {
            myControls.Add(textBox1);
            myControls.Add(textBox2);
            myControls.Add(textBox3);
            

            label7.Text = CountCharacters(textBox1, 100);
            label6.Text = CountCharacters(textBox2, 22);
            label8.Text = CountCharacters(textBox3,8);
            
            ShowErrors();
        }

        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            ValidateFirst();            
        }

        private void textBox2_Validating(object sender, CancelEventArgs e)
        {
            ValidateSecond();
        }

        private void textBox3_Validating(object sender, CancelEventArgs e)
        {
            ValidateThird();
        }

        private void MaxLengthValidation(Control control, int ctrLength, string msg)
        {
            if (control.Text.Length > ctrLength || control.Text.Length == 0)
            {
                errorProvider1.SetError(control, string.Format(msg, ctrLength));
            }
            else
                errorProvider1.SetError(control, null);

            ShowErrors();
        }

        private void ShowErrors()
        {
            string test = "";

            test += SetErrorFormat(textBox1);
            test += SetErrorFormat(textBox2);
            test += SetErrorFormat(textBox3);
            label10.Text = "Listado de errores: \n";
            label10.Text += test;

            if (test != "")
            {
                label10.Visible = true;
            }
            else
            {
                label10.Visible = false;
            }
            ValidateButton(test);
        }

        private string SetErrorFormat(Control control)
        {
            string res;

            if (errorProvider1.GetError(control) != "")
            {
                res = string.Format("- {0} \n", errorProvider1.GetError(control));
            }
            else
                res = "";
            return res;

        }

        private string CountCharacters(Control control, int ctrLength)
        {
            return string.Format("({0}/{1})", control.Text.Length, ctrLength);
        }

        private void LengthValidation(Control control, int ctrLength, string msg)
        {
            if (control.Text.Length != ctrLength)
            {
                errorProvider1.SetError(control, string.Format(msg, ctrLength));
            }
            else
                errorProvider1.SetError(control, null);

            ShowErrors();
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) || char.IsWhiteSpace(e.KeyChar) || char.IsPunctuation(e.KeyChar) || char.IsSymbol(e.KeyChar))
            {
                MessageBox.Show("Solo se permiten numeros");
                e.Handled = true;
                return;
            }

        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) || char.IsWhiteSpace(e.KeyChar) || char.IsPunctuation(e.KeyChar) || char.IsSymbol(e.KeyChar))
            {
                MessageBox.Show("Solo se permiten numeros");
                e.Handled = true;
                return;
            }

        }
         
        private void ValidateButton(string test)
        {
            if (test == "" && myControls.Where(x => x.Text.Length == 0).Count() == 0)
            {
                button1.Enabled = true;
            }
            else
                button1.Enabled = false;
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ValidateFirst();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            ValidateSecond();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            ValidateThird();
        }

      
        private void ValidateFirst()
        {
            MaxLengthValidation(textBox1, 100, "Se deben ingresar por lo menos 1 caracter y hasta {0}");
            label7.Text = CountCharacters(textBox1, 100);
        }

        private void ValidateSecond()
        {
            LengthValidation(textBox2, 22, "Se deben ingresar {0} caracteres numéricos");

            label6.Text = CountCharacters(textBox2, 22);
        }

        private void ValidateThird()
        {
            //IML Credentials.currentUser.LargoReferenciaUnivDelDev
            LengthValidation(textBox3, 8, "Se deben ingresar {0} caracteres");

            label8.Text = CountCharacters(textBox3, 8);
        }

     
    }
}
